<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>流水管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户流水</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item>
          <el-select v-model="queryinfo.comid" placeholder="所属公司" clearable filterable>
            <el-option
                v-for="item in comList"
                :key="item.comid"
                :label="item.title"
                :value="item.comid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryinfo.uid" placeholder="所属用户" clearable filterable>
            <el-option
                v-for="item in userList"
                :key="item.uid"
                :label="item.account"
                :value="item.uid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作">
          <el-select v-model="queryinfo.state" clearable filterable>
            <el-option label="增加" value="1"></el-option>
            <el-option label="减少" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间选择">
          <el-date-picker
              v-model="timer"
              type="datetimerange"
              range-separator="-"
              start-placeholder="Start date"
              end-placeholder="End date"
              :default-time="defaultTime"
              @change="changeData"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>
      </el-form>
      <hr>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="公司" width="180" />
        <el-table-column prop="account" label="用户账号" width="180" />
        <el-table-column prop="type" label="操作类型" width="180" />
        <el-table-column prop="info" label="操作说明" width="180" show-overflow-tooltip />
        <el-table-column prop="befor_bean" label="变更前" width="180" />
        <el-table-column prop="bean" label="变更金额" width="180" />
        <el-table-column prop="after_bean" label="变更后" width="180" />
        <el-table-column prop="addtime" label="操作时间" >
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />
    </el-card>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {ElMessage} from "element-plus";
import axios from "@/utils/axios";
import {formatDate} from '@/utils/date'

export default {
  name: "user",
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,

      //下拉
      userList:[],
      comList:[],


      tableData: [],

      //时间操作
      timer: [],
      //默认时间
      defaultTime: [
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ],
    })
    onMounted(() => {
      GetData()
      GetComUser()
    })


    //获取公司/用户
    const GetComUser = async () => {
      {
        const {data: res} = await axios.get('record/bean/user')
        if (res.code !== 1000) return ElMessage.error(res.msg)
        state.userList = res.data

        const {data: resp} = await axios.get('record/bean/com')
        if (resp.code !== 1000) return ElMessage.error(resp.msg)
        state.comList = resp.data
      }

    }

    //时间操作
    const changeData = async (row) => {
      if (row !== null) {
        state.queryinfo.startTime = ((new Date(row[0])).getTime()) / 1000
        state.queryinfo.endTime = ((new Date(row[1])).getTime()) / 1000
      } else {
        delete state.queryinfo.startTime
        delete state.queryinfo.endTime
      }
    }
    // 获取列表
    const GetData = async () => {
      const {data: res} = await axios.get('record/bean', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      state.queryinfo.page = val
      GetData()
    }
    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }

    return {
      ...toRefs(state),
      GetData,
      formatDate,
      changeData,
      GetComUser,
      changePage,
      handleSizeChange,
    }
  }
}
</script>

<style scoped>

</style>